package com.xinan.system.config;

import java.util.Properties;

import org.apache.log4j.Logger;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.config.Handlers;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.handler.UrlSkipHandler;
import com.jfinal.ext.plugin.quartz.QuartzPlugin;
import com.jfinal.ext.plugin.tablebind.AutoTableBindPlugin;
import com.jfinal.ext.route.AutoBindRoutes;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.xinan.system.interceptor.AuthInterceptor;
import com.xinan.weixin.DemosController;
import com.xinan.utils.PropertiesHelper;
/**
 * API引导式配置
 */
public class AppConfig extends JFinalConfig {
	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		loadPropertyFile("app_config.txt");				// 加载少量必要配置，随后可用getProperty(...)获取值
		me.setDevMode(getPropertyToBoolean("devMode", false));
		me.setViewType(ViewType.FREE_MARKER); 					// 设置视图类型为Jsp，否则默认为FreeMarker
		me.setBaseViewPath("/WEB-INF/views");
		// 加载微信配置
        Properties r = PropertiesHelper.loadProperty("/weixin.properties");
        if(!r.isEmpty()){
            try{
            	ApiConfig.setDevMode(r.getProperty("wxdevMode").equals("true"));  //调试模式 false 关闭
                ApiConfig.setUrl(r.getProperty("wxURL"));
                ApiConfig.setToken(r.getProperty("Token"));
                ApiConfig.setAppId(r.getProperty("AppId"));
                ApiConfig.setAppSecret(r.getProperty("AppSecret"));
            }catch(Exception e){
                Logger.getLogger(AppConfig.class).error("无法读取参数/weixin.properties",e);
            }
        }else{
            Logger.getLogger(AppConfig.class).error("无法读取 weixin.properties");
        }
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
        //采用自动注册方式
        me.add("/weixin", DemosController.class);
        me.add(new AutoBindRoutes());
        
    }
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
        //缓存插件
        me.add(new EhCachePlugin());
//        me.add(new EhCachePlugin("/src/ehcache.xml"));



        //配置数据库连接池插件

        DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbc.url"), getProperty("jdbc.username"), getProperty("jdbc.password").trim());
		druidPlugin.setDriverClass(getProperty("jdbc.driverClass"));
        druidPlugin.setMaxActive(getPropertyToInt("jdbc.maxActive"));
        druidPlugin.setMinIdle(getPropertyToInt("jdbc.minIdle"));
        druidPlugin.setInitialSize(getPropertyToInt("jdbc.initialSize"));
        druidPlugin.setMaxWait(getPropertyToInt("jdbc.maxWait"));
        druidPlugin.setTimeBetweenEvictionRunsMillis(getPropertyToInt("jdbc.timeBetweenEvictionRunsMillis"));
        druidPlugin.setMinEvictableIdleTimeMillis(getPropertyToInt("jdbc.minEvictableIdleTimeMillis"));
        druidPlugin.setValidationQuery(getProperty("jdbc.validationQuery"));
        druidPlugin.setTestWhileIdle(getPropertyToBoolean("jdbc.testWhileIdle"));
        druidPlugin.setTestOnBorrow(getPropertyToBoolean("jdbc.testOnBorrow"));
        druidPlugin.setTestOnReturn(getPropertyToBoolean("jdbc.testOnReturn"));
        //druid监控用
        druidPlugin.addFilter(new StatFilter());
        WallFilter wall = new WallFilter();
        wall.setDbType("mysql");
        druidPlugin.addFilter(wall);

        me.add(druidPlugin);
		//动态Model与表绑定插件
		AutoTableBindPlugin atbp = new AutoTableBindPlugin(druidPlugin);
		atbp.setContainerFactory(new CaseInsensitiveContainerFactory(true));
        atbp.setShowSql(true);
		me.add(atbp);
        //定时任务调度插件
//		QuartzPlugin quartzPlugin =  new QuartzPlugin("job.properties");
//		me.add(quartzPlugin);
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
	    me.add(new AuthInterceptor());
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
        me.add(new ContextPathHandler("ctx"));
        me.add(new UrlSkipHandler("/static/",false));
        DruidStatViewHandler dvh =  new DruidStatViewHandler("/druid");
        me.add(dvh);
//        me.add(new AuthHandler());
	}
	
	/**
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}
}
